<template>
  <el-container class="home-container">
    <!-- 侧边栏 -->
    <el-aside width="200px">
      <el-scrollbar>
        <el-menu :default-active="activeMenu" class="menu" router>
          <!-- 所有用户可见的菜单项 -->

          <el-menu-item index="/packages">
            <el-icon><Calendar /></el-icon>
            <span>体检预约</span>
          </el-menu-item>
          <el-menu-item index="/appointments">
            <el-icon><List /></el-icon>
            <span>我的预约</span>
          </el-menu-item>

          <!-- 管理员可见的菜单项 -->
          <template v-if="isAdmin">
            <el-menu-item index="/admin_users">
              <el-icon><List /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/admin_role">
              <el-icon><List /></el-icon>
              <span>权限管理</span>
            </el-menu-item>
            <el-menu-item index="/admin_medicalInstitutions">
              <el-icon><List /></el-icon>
              <span>机构管理</span>
            </el-menu-item>
            <el-menu-item index="/admin_packages">
              <el-icon><List /></el-icon>
              <span>套餐管理</span>
            </el-menu-item>
            <el-menu-item index="/admin_appointments">
              <el-icon><List /></el-icon>
              <span>预约管理</span>
            </el-menu-item>
            <el-menu-item index="/admin_evaluations">
              <el-icon><List /></el-icon>
              <span>评价管理</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-scrollbar>
    </el-aside>

    <!-- 主容器 -->
    <el-container>
      <!-- 顶部栏 -->
      <el-header class="header">
        <div class="logo">
          <h2>医疗体检预约系统</h2>
        </div>
        <div class="user-info">
          <el-dropdown>
            <span class="user-dropdown">
              {{ userName }}
              <el-icon><CaretBottom /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人中心</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主要内容区 -->
      <el-main class="main-content">
        <router-view />
      </el-main>

      <!-- 底部 -->
      <el-footer class="footer">
        © 2024 医疗体检预约系统 版权所有
      </el-footer>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Calendar, List, CaretBottom } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const activeMenu = ref('/packages')
const userName = ref('用户名') // 这里可以从用户store中获取
const userStore = useUserStore()

// 检查是否是管理员
const isAdmin = userStore.hasRole('admin')
const isUser = userStore.hasRole('user')

// 退出登录
const handleLogout = async () => {
  try {
    await userStore.userLogout()
    router.push('/login')
  } catch (error) {
    console.error('退出登录失败:', error)
  }
}
</script>

<style lang="scss" scoped>
.home-container {
  height: 100vh;

  .el-aside {
    background-color: #304156;

    .menu {
      border-right: none;
      background-color: transparent;
    }
  }

  .header {
    background-color: #fff;
    box-shadow: 0 1px 4px rgba(0,21,41,.08);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    height: 60px;

    .logo {
      h2 {
        margin: 0;
        color: #303133;
      }
    }

    .user-info {
      .user-dropdown {
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 8px;
        color: #606266;
      }
    }
  }

  .main-content {
    background-color: #f0f2f5;
    padding: 20px;
  }

  .footer {
    text-align: center;
    color: #909399;
    padding: 20px 0;
    background-color: #fff;
  }
}

// 深度修改 element-plus 菜单样式
:deep(.el-menu) {
  .el-menu-item {
    color: #bfcbd9;

    &:hover, &.is-active {
      color: #fff;
      background-color: #263445;
    }
  }

  .el-menu-item [class^="el-icon"] {
    color: inherit;
  }
}
</style>
